package com.versita.domain;


import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * 
 * This 'extends keyword' makes UserLocation its own table and has the user's id
 * attached to its primary key.
 * 
 * @author vmc
 *
 */

@Entity
public class UserLocation {
    /**
     * 
     */
    private static final long serialVersionUID = -8274156813117667010L;

    private long userLocationId;
    private GPS gps;
    private Date date = new Date(System.currentTimeMillis());

    private List<UserLocationHistory> userLocationHistory = new LinkedList<UserLocationHistory>();

    /**
     * @return the userLocationId
     */
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    public long getUserLocationId() {
        return userLocationId;
    }

    /**
     * @param userLocationId the userLocationId to set
     */
    public void setUserLocationId(long userLocationId) {
        this.userLocationId = userLocationId;
    }

    /**
     * @return the gps
     */
    @Embedded
    public GPS getGps() {
        return this.gps;
    }

    /**
     * @param gps the gps to set
     */
    public void setGps(GPS gps) {
        this.gps = gps;
        this.date = new Date(System.currentTimeMillis());
    }

    /**
     * @return the date
     */
    @Temporal(value = TemporalType.TIMESTAMP)
    public Date getDate() {
        return this.date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(Date date) {
        this.date = date;
    }

    /**
     * @return the userLocationHistory
     */
    @OneToMany
    public List<UserLocationHistory> getUserLocationHistory() {
        return userLocationHistory;
    }

    /**
     * @param userLocationHistory the userLocationHistory to set
     */
    public void setUserLocationHistory(List<UserLocationHistory> userLocationHistory) {
        this.userLocationHistory = userLocationHistory;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((this.date == null) ? 0 : this.date.hashCode());
        result = prime * result + ((this.gps == null) ? 0 : this.gps.hashCode());
        return result;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (getClass() != obj.getClass())
            return false;
        UserLocation other = (UserLocation) obj;
        if (this.date == null) {
            if (other.date != null)
                return false;
        } else if (!this.date.equals(other.date))
            return false;
        if (this.gps == null) {
            if (other.gps != null)
                return false;
        } else if (!this.gps.equals(other.gps))
            return false;
        return true;
    }
}
